package com.example.demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(5)
public class CheckNameAspect {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	ThreadLocal<Long> startTime = new ThreadLocal<>();

	// 切入点
	@Pointcut("execution(public * com.example.demo..*.*(..))")
	public void checkname() {
	}

	// 在切入点开始处切入内容
	@Before("checkname()")
	public void doBefore(JoinPoint joinPoint) throws Throwable {
		startTime.set(System.currentTimeMillis());
		logger.info("CheckNameAspect -》Before");
	}

	// 使用@After在切入点结尾处切入内容
	@After("checkname()")
	public void doAfter(JoinPoint joinPoint) {
		logger.info("CheckNameAspect -》doAfter");

	}

	// 使用@AfterReturning在切入点return内容之后切入内容（可以用来对处理返回值做一些加工处理）
	@AfterReturning(returning = "ret", pointcut = "checkname()")
	public void doAfterReturning(Object ret) throws Throwable {
		// 处理完请求，返回内容
		logger.info("CheckNameAspect-》RESPONSE : " + ret);
		logger.info("CheckNameAspect-》SPEND TIME : " + (System.currentTimeMillis() - startTime.get()));
	}
}
